#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

struct tvar
{
  int num;
  char str[256];
};
// 主子线程共享全局变量

void *thread_func(void *arg)
{
  struct tvar *var;
  var = malloc(sizeof(struct tvar));
  var->num = 100;
  strcpy(var->str, "hello world");
  pthread_exit((void *)var);
}

int main()
{

  pthread_t id;
  int ret;
  struct tvar *var;
  pthread_create(&id, NULL, thread_func, NULL);
  if (ret != 0)
  {
    perror("pthread_create");
    exit(1);
  }

  // 回收子线程
  ret = pthread_join(id, (void **)&var); // 传出参数需要指针来接受,返回的是 struct tvar *类型,那么需要struct tvar **指针来接受
  if (ret != 0)
  {
    perror("pthread_join");
    exit(1);
  }
  printf("num=%d, str=%s\n", var->num, var->str);

  pthread_exit(NULL); // 退出主线程
}